package com.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import hyl.base.cache.redis.CRedis;
import hyl.base.cache.redis.RedisPool;

import hyl.core.Amy;
import hyl.core.db.MyDB;
import hyl.core.run.MyTimer;
import hyl.ext.base.MySession;
import hyl.ext.base.SessionFactory;
import redis.clients.jedis.Pipeline;

public class My extends Amy {
	protected static My my;

	// public static String DB2 = "mysql2";
	static {
		DB1 = "mysql1";
	}

	// protected static MyKafka _kfkp;
	// public static MyDB _timerdb1= MyDB.createCommitDB(DB1);
	private My() {
		// MyPath.test(MyPath.class);
		M.LRole = M2.getRoles();
		SessionFactory.getInstance();
		RedisPool.getInstance();
	}

	/*
	 * public static MyKafka getKfkP() { return _kfkp; }
	 */

	public static My getInstance() {
		// String path=MyPath.getLastPathOfClassLoader(My.class.getClassLoader());
		// MyConfig conf=MyConfig.deserial(path);
		if (my == null) {
			my = new My();

		}
		// MyConfig.serial(path);
		// _kfkp = MyKafka.getInsProducer();
		// getRewardService();
		return my;
	}

	public static MyDB createCommitDb2() {
		// 通过连接创建DB对象 操作sql
		return MyDB.createCommitDB(DB1);
	}

	public static void reset() {
		my = new My();
		// _rewardService = new RewardService2();
		MyTimer.close("日同步");
	}

	// 判断权限
	public static void checkRole(String JSPTITLE, HttpServletRequest req, HttpServletResponse res) throws IOException {
		MySession ses = getSession(req);
		String quanxian = ses.tag.get("quanxian").toString();
		if (quanxian.indexOf(JSPTITLE.trim() + ",") < 0) {
			res.sendRedirect(req.getContextPath() + M.URL_404);
		}
	}

	/**
	 * 适用于直接访问的jsp页面
	 */
	public static MySession getSession(HttpServletRequest req, HttpServletResponse res) {
		return SessionFactory.getSession(req, res);
	}

	/**
	 * 适用于sping control
	 */
	public static MySession getSession(HttpServletRequest req) {
		return ((MySession) req.getAttribute(SessionFactory.SessionGROUP));
	}

	/**
	 * 适用于直接访问的jsp页面
	 */
	public static int getMySessionUserID(HttpServletRequest req, HttpServletResponse res) {
		MySession ses = getSession(req, res);
		return ses.getUserId();
	}

	// 2019
	public static int getLanguage(HttpServletRequest req, HttpServletResponse res) {
		return SessionFactory.getSession(req, res).getLang();
	}

	/**
	 * 适用于直接访问的jsp页面 2019.3.5 sx
	 */
	public static int getMySessionRoleid(HttpServletRequest req, HttpServletResponse res) {
		MySession ses = getSession(req, res);
		return ses.getRoleid();
	}

	// 2019
	public static int getMySessionRoleId(HttpServletRequest req) {
		MySession ses = getSession(req);
		return ses.getRoleid();
	}

	// 2019
	public static int getMySessionUserID(HttpServletRequest req) {
		MySession ses = getSession(req);
		return ses.getUserId();
	}

	// 2019
	public static MySession getSession(String uuid) {
		return SessionFactory.getSessionById(uuid);
	}

	public synchronized static Pipeline getRd1(int idx) {
		// 通过连接创建DB对象 操作sql
		return RedisPool.getInstance().getRedis("redis1", idx).createPipeline();
	}

	public synchronized static CRedis createRd1(int idx) {
		// 通过连接创建DB对象 操作sql
		return RedisPool.getInstance().getRedis("redis1", idx);
	}

	public static void exit() {
		closeAllThreads();
		closeDb();
		closeRd();
	}

	private static void closeRd() {
		RedisPool.getInstance().close();
		Log.info("redis 连接池正常关闭...");
	}

	public static void onErrClose() {
		onExit = new Runnable() {
			public void run() {
				Log.info("异常关闭触发事件");
				exit();
			}
		};
	}

}
